//
//  CBSettingViewController.m
//  CROSBY
//
//  Created by 王华磊 on 2017/10/3.
//  Copyright © 2017年 Haley. All rights reserved.
//

#import "CBSettingViewController.h"
#import "CBDeviceViewController.h"
#import "CBWebViewController.h"
#import "GWSettingCell.h"
#import "BluetoothManager.h"
#import "NSString+QCExtension.h"
#import "ProgressHUD.h"

#import <AudioToolbox/AudioToolbox.h>

@interface CBSettingViewController ()<UITableViewDataSource,UITableViewDelegate,GWSettingCellDelegate>

@property (nonatomic, strong) NSMutableArray  *datas;

@property (strong, nonatomic) NSMutableArray  *section2;

@property (strong, nonatomic) UITableView *tableView;

@end

@implementation CBSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = showText(@"设置");
    
    [self p_initData];
    
    [self initTableView];
}

- (void)initTableView
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 200)];
    headerView.backgroundColor = [UIColor clearColor];
    
    UIImageView *logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake((kWidth-100)*0.5, 50, 100, 100)];
    logoImageView.image = [UIImage imageNamed:@"launcher"];
    logoImageView.layer.cornerRadius = 10;
    logoImageView.layer.masksToBounds = YES;
    [headerView addSubview:logoImageView];
    
    UILabel *versionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, kWidth, 20)];
    versionLabel.textColor = [UIColor whiteColor];
    versionLabel.textAlignment = NSTextAlignmentCenter;
    NSDictionary *appInfo = [[NSBundle mainBundle] infoDictionary];
    NSString *version = [appInfo objectForKey:@"CFBundleShortVersionString"];
    versionLabel.text = version;
    [headerView addSubview:versionLabel];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight-64-49-kTopSafeHeight-kBottomSafeHeight) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.tableView.rowHeight = 50;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableHeaderView = headerView;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self.tableView selector:@selector(reloadData) name:kConnectedDeviceListChange object:nil];
    
    [self.tableView reloadData];
}

- (void)setupLanguage
{
    self.navigationItem.title = showText(@"设置");
    
    [self p_initData];
    
    [self.tableView reloadData];
}

- (void)p_initData
{
    NSString *content = showText(@"未连接");
    NSUInteger count = [BluetoothManager standardManager].address.count;
    if (count > 0) {
        content = [NSString stringWithFormat:@"%@(%d)",showText(@"已连接"),(int)count];
    }
    NSDictionary *dict1 = @{@"icon":@"settting_devive_connect",
                            @"title":@"设备连接",
                            @"accessType":@(AccessViewTypeLabel),
                            @"content":content};
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *language = [userDefaults stringForKey:kChosenLanguage] ? : @"";
    NSDictionary *dict2 = @{@"icon":@"setting_lanuage",
                            @"title":@"语言",
                            @"accessType":@(AccessViewTypeLabel),
                            @"content":language};
    NSDictionary *dict3 = @{@"icon":@"crosby_instagram",
                            @"title":@"CROSBY KIDS Instagram",
                            @"accessType":@(AccessViewTypeLabel)};
    
    NSArray *data = @[dict1, dict2,dict3];
    self.datas = [[NSMutableArray alloc] initWithArray:data];
}

- (void)gotoInstagram
{
    NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
    // 如果安装了instagram，则打开app
    if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
        NSURL *userNameURL = [NSURL URLWithString:@"instagram://user?username=kids_crosby"];
        [[UIApplication sharedApplication] openURL:userNameURL];
        return;
    }
    
    //    NSString *url = @"https://www.instagram.com/crosby.ru/";
    CBWebViewController *webVC = [[CBWebViewController alloc] init];
    webVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:webVC animated:YES];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.datas count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GWSettingCell *cell = [GWSettingCell cellWithTableView:tableView];
    cell.delegate = self;
    NSDictionary *dict = self.datas[indexPath.row];
    cell.dict = dict;
    
    return cell;
}


#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        CBDeviceViewController *deviceVC = [[CBDeviceViewController alloc] init];
        deviceVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:deviceVC animated:YES];
    } else if (indexPath.row == 1) {
        [self showChoseLanguageView];
    } else if (indexPath.row == 2) {
        [self gotoInstagram];
    }
}

#pragma mark - GWSettingCellDelegate
- (void)switchChange:(UISwitch *)switchView onCell:(GWSettingCell *)cell
{

}

@end
